import React, { Component } from 'react'
import PropTypes from 'prop-types';
import { nanoid } from "nanoid";
export class Header extends Component {

    // 对于接受的props 进行限制
	static propTypes = {
		addTodo: PropTypes.func.isRequired
	}

    handKeyUp=(event)=>{
        if(event.keyCode != 13) {
            return
        } 

        if( event.target.value.trim() == ""){
            alert("输入为空,不可添加");
            return;
        }

        // event.target.value;
        let obj = { id: nanoid(), name: event.target.value, done: false };
        //todoobj 传递
        this.props.addTodo(obj);
        console.log('Header 传递完成');

        event.target.value = '';
    }

    render() {
        return (
            <div className="todo-header">
                <input onKeyUp={this.handKeyUp} type="text" placeholder="请输入你的任务名称，按回车键确认"/>
            </div>
        )
    }
}
